package com.finger.fakegps.hook;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

//http://api.cellocation.com/recell/?lat=39.5324&lon=116.3424&n=1&mnc=0

public class LocationHook implements IXposedHookLoadPackage {

	@Override
	public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {

		String packageName = loadPackageParam.packageName;
		if (!"com.tencent.mm".equals(packageName)) {
			return;
		}

		Location location = getLocation();
		if (location != null) {
			double longitude = location.getLon();// 经度
			double latitude = location.getLat();// 纬度
			int mcc = 460;
			int mnc = location.getMnc();
			int lac = location.getLac();// 位置区域码
			int cid = location.getCid();// 基站编号

			XposedBridge.log("模拟定位:" + location.toString());
			HookUtils.HookAndChange(loadPackageParam.classLoader, latitude, longitude, mcc, mnc, lac, cid);
		}
	}

	/**
	 * 读取配置信息
	 * 
	 * @return
	 */
	private Location getLocation() {
		Location location = null;
		int is_open = PreferencesUtils.getInt("is_open", 0);
		if (is_open == 1) {
			String lat = PreferencesUtils.getString("lat", "0.0");
			String lon = PreferencesUtils.getString("lon", "0.0");
			String mnc = PreferencesUtils.getString("mnc", "0");
			String lac = PreferencesUtils.getString("lac", "0");
			String cid = PreferencesUtils.getString("cid", "0");

			location = new Location(Double.parseDouble(lon), Double.parseDouble(lat), 460, Integer.parseInt(mnc),
					Integer.parseInt(lac), Integer.parseInt(cid));
		}

		return location;
	}
}
